package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Beans.Member;
import ServiceInterface.ServiceProxy;

/**
 * Servlet implementation class SignUp
 */
public class SignUp extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ServiceProxy proxy = new ServiceProxy();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SignUp() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		int qdone;
		String delim = "[$]";
		HttpSession session = request.getSession();
			
		try {
			String first_name = request.getParameter("first_name");
			String last_name = request.getParameter("last_name");
			String address = request.getParameter("address");
			String city = request.getParameter("city");
			String state = request.getParameter("state");
			String zip = request.getParameter("zip");
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			String type = request.getParameter("type");
			
			System.out.println("Password in SignUp.java: " + password);
			
			Member member = new Member();
			member.setEmail(email);
			member.setPassword(password);
			member.setFirst(first_name);
			member.setLast(last_name);
			member.setAddress(address);
			member.setCity(city);
			member.setState(state);
			member.setZip(Integer.parseInt(zip));
			member.setType(type);
			member.setFees(0);
			member.setOutstandingMovies(0);
		 
			System.out.println("Password in Member" + member.getPassword());
			
			proxy.setEndpoint("http://localhost:8079/VLMS3/services/Service");
			
			qdone = proxy.createAccount(member);
			
			System.out.println("after proxy");

			
			if (qdone == 0) {
				session.setAttribute("alert", "Sign up failed! Please try again.");
				response.sendRedirect("SignUp.jsp");
			}
			else {
				session.setAttribute("userSession", session);
				session.setAttribute("email", email);
				String alert = "Successfully Signed Up!";
				session.setAttribute("alert", alert);
				response.sendRedirect("AllMovies.jsp"); 
			}
				
		} catch (Exception e) {
			e.printStackTrace();
			session.setAttribute("alert", "Sign Up failed. Please try again later.");
			response.sendRedirect("SignUp.jsp");
		}
		finally {
			out.close();
		}
			
		
	}

}
